package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmaelDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmaelDishMapper setmaelDishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page page = (Page) setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        List<SetmealVO> setmealVOS = new ArrayList<>();
        for (Setmeal setmeal : result) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            String setmealName = categoryMapper.selectByName(setmeal.getCategoryId());
            setmealVO.setCategoryName(setmealName);
            setmealVOS.add(setmealVO);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(setmealVOS);
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //添加菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmaelDishMapper.insertSetmealDish(setmealDishes);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //修改套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!= null && !setmealDishes.isEmpty()){
            //删除套餐菜品
            setmaelDishMapper.delete(Arrays.asList(setmealDTO.getId()));
        for (SetmealDish setmealDish : setmealDishes) {
            //添加套餐菜品
            setmealDish.setSetmealId(setmealDTO.getId());
        }
            setmaelDishMapper.insert(setmealDishes);
        }
    }

    @Override
    public SetmealVO getByid(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //查询套餐
        Setmeal setmeal = setmealMapper.getBySetmealId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //查询套餐菜品
        List<SetmealDish> byid = setmaelDishMapper.getByid(id);
        setmealVO.setSetmealDishes(byid);
        return setmealVO;
    }

    @Override
    public void status(Integer status, Long id) {
        //起售停售
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    @Override
    //进行事务管理
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        //前端未做非空判断 在后端做个判断
        if (ids == null || ids.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.SELETE_DELETE);
        }
        //判断套餐是否起售
        Integer statusCont = setmealMapper.getByids(ids);
        if (statusCont > StatusConstant.DISABLE){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
            //删除套餐
            setmealMapper.delete(ids);
            //删除套餐关联的菜品
            setmaelDishMapper.delete(ids);


    }

    @Override
    public  List<Setmeal> list(Long categoryId) {
        List<Setmeal> bySetmealId = setmealMapper.setmealList(categoryId);
        return bySetmealId;
    }

    @Override
    public List<DishItemVO> setmealList(Long id) {
        List<DishItemVO> dishItemVO = setmaelDishMapper.setmealList(id);
        return dishItemVO;
    }
}
